package com.jiuyuan.config.security;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.jiuyuan.entity.vo.RespBean;
import com.jiuyuan.entity.vo.RespBeanEnum;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @program: hrms
 * @description:
 * @author: Feng
 * @create: 2023-02-15 13:17
 **/

@Component
public class RestAccessDeniedHandler implements AccessDeniedHandler {
    @Override
    // 权限不足，输出
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        response.setContentType("application/json");
        response.setCharacterEncoding("utf-8");
        RespBean respBean = RespBean.error(RespBeanEnum.AUTHORITY_DENIED);
        PrintWriter writer = response.getWriter();
        writer.write(new ObjectMapper().writeValueAsString(respBean));
        writer.flush();
        writer.close();
    }
}
